#coding=utf-8

from selenium import webdriver
from appium import webdriver
from time import sleep
from selenium.common.exceptions import WebDriverException
import os
from nav_plugin_op import nav_plugin_factory
from Public import BasePage
from Tool.usefulTools import userfulToolsFactory
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import settings

#实例化基础数据

navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_oprate_instance = element_Oprate()
basicAction = BasicAction()
contactor_Paramter_instances = BaseConstant.contactor_Paramter
Leaderassign_Paramter_instances = BaseConstant.Leaderassign_Paramter
#nav_name = "消息"
nav_name = ["导航栏联系人",'xpath',"//android.widget.LinearLayout[@resource-id='cn.com.egova.egovamobile:id/base_tab_tabs_bottom']/android.widget.LinearLayout[3]/android.widget.FrameLayout[1]/android.widget.RadioButton[1]"]
sendmessage_keys_name = "输入文字"

class contactor_factory():

	def initiate_chat(self,driver,file_path):
		try:
			# 点击消息的导航，发起聊天，建群
			element_oprate_instance.element_click(nav_name,file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.actionbar_id, file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.start_talk_name, file_path)
			#点击选择部门
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.choose_unit, settings.pic_file_path)
			# 勾选第一个部门
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.first_unit_checkbox,settings.pic_file_path)
			# 点击确认按钮
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_confirm_unit, file_path)
			sleep(2)

		# 	if element_oprate_instance.element_display_commit(contactor_Paramter_instances.select_contacts_name.decode("utf-8"), file_path):
		# 		element_oprate_instance.element_click(contactor_Paramter_instances.choose_a_part_id, file_path)
		# 		element_oprate_instance.element_click(contactor_Paramter_instances.selet_a_xpath, file_path)
		# 		# element_oprate_instance.element_click(contactor_Paramter_instances.selet_b_xpath, file_path)
		# 		element_oprate_instance.element_click(contactor_Paramter_instances.contact_confirm_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'initiate_chat_successful')
		# 	else:
		# 		print 'test failed'
        #
		except WebDriverException,el_error:
			element_oprate_instance.element_save_pic(file_path, 'initiate_chat_fail')
			print el_error
			return False

	def chat_window(self,driver,file_path):
			#发消息
		try:
			#表情输入
			element_oprate_instance.element_click(contactor_Paramter_instances.selectemotion_id, file_path)
			sleep(1)
			element_oprate_instance.element_click(contactor_Paramter_instances.emotion_keys_xpath, file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.btn_send_id, file_path)
			#文字输入
			element_oprate_instance.element_click(contactor_Paramter_instances.sendmessage_id, file_path)
			element_oprate_instance.element_send_keys(contactor_Paramter_instances.sendmessage_id,sendmessage_keys_name.decode("utf-8"), file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.btn_send_id, file_path)
			#语音输入
			element_oprate_instance.element_click(contactor_Paramter_instances.btn_voice_id, file_path)
			sleep(1)
			driver.tap([(360,1050)],3000)
			sleep(4)
			#添加图片
			element_oprate_instance.element_click(contactor_Paramter_instances.btn_add_id, file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.photo_add_name, file_path)
			sleep(2)
			#选择第一个图片
			element_oprate_instance.element_click(contactor_Paramter_instances.first_photo_select_button, file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.select_id, file_path)
			element_oprate_instance.element_click(contactor_Paramter_instances.confirm_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'chat_window_successful')
			element_oprate_instance.element_click(contactor_Paramter_instances.exit_id, file_path)
			sleep(2)

		except WebDriverException,el_error:
			element_oprate_instance.element_save_pic(file_path, 'chat_window_fail')
			print el_error
			return False

if __name__ == '__main__':
	print u'联系人'


